﻿package nl.haemers.utils 
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * @author Jonathan Haemers
	 * @company Idreams
	 */
	public class XMLLoader extends EventDispatcher
	{
		private var loader		:URLLoader;
		private var request		:URLRequest;
		private var _randomizer	:Boolean;
		private var _data		:XML;
		/**
		 * 
		 */
		public function XMLLoader( url:String, randomizer:Boolean=true ) 
		{
			_randomizer = randomizer;
			if(_randomizer)
				url = url +"?random="+ randomize();
			request = new URLRequest(url);
			loader = new URLLoader();
			configLoader( loader );
		}
		
		public function load() : void
		{
			loader.load( request );
		}
		
		public function get data() : XML
		{
			return _data;
		}
		
		public function dispose() : void
		{
			_data = null;
			removeListeners( loader );
			loader = null;
			request = null;
		}
		
		private function configLoader( d:EventDispatcher ) : void
		{
			d.addEventListener(Event.OPEN, openHandler);
			d.addEventListener(Event.COMPLETE, completeHandler);
            d.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            d.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            d.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            d.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
		private function removeListeners( d:EventDispatcher ) : void
		{
			d.removeEventListener(Event.OPEN, openHandler);
			d.removeEventListener(Event.COMPLETE, completeHandler);
            d.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
            d.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            d.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            d.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
		private function openHandler( e:Event ) : void
		{
			//trace("Open: " + e);
			if (hasEventListener(Event.OPEN))
			{
				dispatchEvent(e);
			}
		}
		
		private function completeHandler(e:Event):void 
		{
			_data = new XML(e.target.data);
            dispatchEvent(e);
        }

        private function progressHandler(e:ProgressEvent) : void 
		{
            //trace("progressHandler loaded:" + e.bytesLoaded + " total: " + e.bytesTotal);
			if (hasEventListener(ProgressEvent.PROGRESS))
			{
				dispatchEvent(e);
			}
        }

        private function securityErrorHandler(e:SecurityErrorEvent) : void 
		{
            //trace("securityErrorHandler: " + e);
			if (hasEventListener(SecurityErrorEvent.SECURITY_ERROR))
			{
				dispatchEvent(e);
			}
        }

        private function httpStatusHandler(e:HTTPStatusEvent) : void 
		{
            //trace("httpStatusHandler: " + e);
			if (hasEventListener(HTTPStatusEvent.HTTP_STATUS))
			{
				dispatchEvent(e);
			}
        }

        private function ioErrorHandler(e:IOErrorEvent) : void 
		{
            trace("ioErrorHandler: " + e);
			if (hasEventListener(IOErrorEvent.IO_ERROR))
			{
				dispatchEvent(e);
			}
        }
		
		private function randomize() : String
		{
			return (Math.random() * 100).toString();
		}
	}
	
}